package com.csu.controller;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;


@RequestMapping("/iodemo")
@RestController
public class AudioController {
    @RequestMapping("/test")
    public void test(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //创建连接对象
        URL url = new URL("//player.bilibili.com/player.html?aid=605187014&bvid=BV1X84y1y74B&cid=893687279&page=1");
        URLConnection conn = url.openConnection();
        //设置超时
        conn.setConnectTimeout(1000);
        conn.setReadTimeout(5000);
        //发起连接
        conn.connect();
        //获取流
        InputStream inputStream = conn.getInputStream();

        //流转换
        IOUtils.copy(inputStream,response.getOutputStream());
        //设置返回类型
        response.addHeader("Content-Type", "audio/mpeg;charset=utf-8");

        response.flushBuffer();

    }
}
